package com.springboot.product.service;

import com.springboot.product.entities.Product;
import com.springboot.product.repo.ProductRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductRepo repo;

    @Override
    public Product saveOrUpdateProduct(Product product) {
        return repo.save(product);
    }

    @Override
    public void deleteProductById(Long id) {
        repo.deleteById(id);
    }

    @Override
    public Product getById(Long id) {
        return repo.findById(id).get();
    }

    @Override
    public Product getProductById(String ID) {
        Long id = Long.parseLong(ID);
        return repo.findById(id).get();
    }

    @Override
    public List<Product> findAllProducts() {
        return repo.findAll();
    }

    @Override
    public List<Product> findByName(String name) {
        return repo.findByName(name);
    }
}
